package com.nbjtjc.safe.api.qrCode;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.mapper.danger.DangerInspectMapper;
import com.nbjtjc.safe.model.danger.DangerInspect;
import com.nbjtjc.safe.qrCode.PngDPIProcessor;
import com.nbjtjc.safe.qrCode.QRCode;
import com.nbjtjc.safe.qrCode.QrCodeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @auther guqianbin
 * @date 2018/9/21 15:12
 * 二维码的接口
 **/
@RestController
@RequestMapping("/qrCode")
@CrossOrigin("*")
public class QrCodeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(QrCodeController.class);


    /**
     * 总列数
     */
    private static final int columnNum = 1;

    @Autowired
    private PngDPIProcessor pngDPIProcessor;


    private int backgroundColor = 0xFFFFFFFF;

    private int codeColor = 0x00000000;

    private int hintMargin = 0;


    @Value("${qr.dpi}")
    private String dpi;

    @Value("${qr.fileSuffix:png}")
    private String fileSuffix;

    @Value("${qr.safeCodePrix:safe}")
    private String safeCodePrix;

    @Value("${root.qrCodeRootPath}")
    private String qrCodeRootPath;

    @Autowired
    private QrCodeService qrCodeService;
    @Autowired
    private DangerInspectMapper dangerInspectMapper;
    @ApiOperation(value = "在线生成单个二维码接口，返回图片")
    @RequestMapping(value = "inspectQrCode", method = { RequestMethod.GET, RequestMethod.POST})
    public void inspectQrCode(HttpServletResponse response, @ApiParam("二维码号，输入10011") Integer inspectId
            , @ApiParam("二维码前缀，1200或者2200") @RequestParam("codePrefix") String type) throws Exception {
        if (ObjectUtil.isNull(inspectId)) {
            return;
        }
        DangerInspect dangerInspect=dangerInspectMapper.selectByPrimaryKey(inspectId);
        if(ObjectUtil.isNull(dangerInspect)){
            return ;
        }
        String fileName=dangerInspect.getName()+".png";
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//        response.setContentType("application/octet-stream");
        try (ServletOutputStream out = response.getOutputStream()) {
            QRCode qrCode = new QRCode.Builder().setBackgroundColor(backgroundColor).setCodeColor(codeColor)
                    .setOutputBitmapPadding(hintMargin).build();
            String codePrix = type;
            writeQRCode(qrCode, qrCodeService.getQrCodeJsonStr(inspectId), dangerInspect.getName(), out);
        } catch (IOException e) {
            LOGGER.error("生成二维码失败！");
        }
    }

    @ApiOperation(value = "下载公司的所有隐患检查表的二维码图片")
    @RequestMapping(value = "downCompanyInspectQrCode", method = { RequestMethod.GET, RequestMethod.POST})
    public void downCompanyInspectQrCode(){

    }

    /**
     *
     * @param qrCode
     * @param content  二维码内容
     * @param code   二维码下面的一行字
     * @param output
     */
    private void writeQRCode(QRCode qrCode, String content, String code, OutputStream output) {
        try {
            ImageIO.write(pngDPIProcessor.process(qrCode.encodeQr(content, code), 300), fileSuffix, output);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
